package main

import (
	"context"
	"encoding/json"
	"event-demo/eventsys"
	"fmt"
)

const (
	EventTypeStartGray     eventsys.EventType = "开始灰度(改成英文)"
	EventTypeCancelGray    eventsys.EventType = "取消灰度"
	EventTypeExpireOffline eventsys.EventType = "计划过期"
	EventTypePausePlan     eventsys.EventType = "计划暂停"
	EventTypeResumePlan    eventsys.EventType = "计划取消暂停"
)

type TestBo struct {
	Task     any
	Resource any
	Rule     any
}

func FeiShuHandler(ctx context.Context, event eventsys.Event[any]) {
	fmt.Printf("[FeiShuHandler] 事件已触发，正在推送给关注人: %v\n", event.Type)
	fmt.Println("开始")

	var bo *TestBo = event.Data.(*TestBo)
	str := ToString(bo)
	fmt.Println(str)
	// 调用飞书
}

func OtherHandler(ctx context.Context, event eventsys.Event[any]) {
	fmt.Print("[OtherHandler] 事件已触发 \n")
	fmt.Println("开始")
}

func main() {
	// 中文改成英文
	// main 里面初始化
	dispatcher := eventsys.NewDispatcher()
	dispatcher.Register(EventTypeStartGray, FeiShuHandler)
	dispatcher.Register(EventTypeCancelGray, FeiShuHandler)
	//
	dispatcher.Register(EventTypeStartGray, OtherHandler)

	fmt.Println("Hello World")

	//sql 事务结束后的操作
	// 定义 事件和对于的bo数据
	e1 := eventsys.Event[any]{
		Type: EventTypeStartGray,
		Data: &TestBo{Task: "任务数据", Rule: "规则数据"},
	}

	dispatcher.Dispatch(context.Background(), e1)
}

func ToString(a any) string {
	b, _ := json.Marshal(a)
	return string(b)
}
